<?php
/**
 * 本文件用于查询用户信息
 *
 * 使用方式
 * <code>
 *    //比如需要查询user_id为123的昵称和性别信息以及30x30头象，用
 *    UCModUserInfo::getUserInfo(123, array(USERINFO_NICKNAME, USERINFO_GENDER_DESC, USERINFO_AVATAR_30);
 *    返回 array(USERINFO_NICKNAME => "张三",
 *                     USERINFO_GENDER_DESC => "男",
 *                     USERINFO_AVATAR_30 => "http://i.sdo.com/xxx.png");
 *
 *    //同时也支持批量查询接口
 *    UCModUserInfo::getUserInfos(array(123, 456), array(USERINFO_NICKNAME, USERINFO_GENDER_DESC, USERINFO_AVATAR_30);
 *    返回是一个user_id => userinfo的一个数组，其中userinfo如上面描述;
 * </code>
 *
 * @author    徐鹏程 <xupengcheng@snda.com>
 * @version   0.0.1		2010/03/29
 */

require_once(dirname(__FILE__).'/../../../unified/SDOApiPeopleService.php');
require_once(dirname(__FILE__).'/../../../unified/SDOApiUCProfilePrivilegeService.php');
require_once(dirname(__FILE__).'/../../libraries/UCLibCodes.php');
require_once(dirname(__FILE__).'/../../libraries/UCLibLocation.php');
require_once(dirname(__FILE__).'/../../libraries/UCFuncUtils.php');
require_once(dirname(__FILE__).'/../../libraries/UCLibLogger.php');
require_once(dirname(__FILE__).'/../../libraries/UCLibCache.php');
require_once(dirname(__FILE__).'/../relation/UCModBlacklist.php');
require_once(dirname(__FILE__).'/../relation/UCModRelation.php');
require_once(dirname(__FILE__).'/../../config/UCConfRelation.php');
require_once(dirname(__FILE__).'/../../config/UCConfMacro.php');


/**
 * 用户资料字段名的定义
 * 其中：
 *   _USERINFO_STATRT和_USERINFO_END 用于校验数据，请勿使用
  */

define('_USERINFO_BASIC_START', 1);
define('USERINFO_UID', 0); // UID
define('USERINFO_NICKNAME', 1); // 昵称
//define('USERINFO_NICKNAME_LAST_UPDATE_TIME', 2); // 昵称的上次修改时间
define('USERINFO_AVATAR_VERSION', 3); // 头像版本
define('USERINFO_GENDER', 4); // 性别数字id
define('USERINFO_GENDER_DESC', 100); // 性别的描述
define('USERINFO_BIRTH_DAY', 5); // 生日
define('USERINFO_BLOOD', 6); // 血型数字id
define('USERINFO_BLOOD_DESC', 101); // 血型的描述
define('USERINFO_NATIONALITY', 7); // 民族
define('USERINFO_NATIONALITY_DESC', 102); // 民族的描述
define('USERINFO_LOCATION', 8); // 居住地
define('USERINFO_LOCATION_DESC', 103); // 居住地的描述
define('USERINFO_HOMETOWN', 9); // 出生地
define('USERINFO_HOMETOWN_DESC', 104); // 出生地的描述
define('USERINFO_REG_TIME', 10); // 注册开通时间
define('USERINFO_REG_IP', 11); // 注册IP
define('USERINFO_SEARCHABLE', 12); // 是否可以搜索到我
define('USERINFO_KEYWORDS', 13); // 个人关键词
define('USERINFO_ACTIVE_FLAG', 14); // 激活状态
define('USERINFO_AUTHED_FLAG', 15); // 认证状态
define('USERINFO_LOCKED_FLAG', 16); // 冻结状态

define('_USERINFO_BASIC_END', 16);

// 头像类型定义
define('USERINFO_AVATAR_30', 200); // 30 x 30
define('USERINFO_AVATAR_45', 201); // 45 x 45
define('USERINFO_AVATAR_90', 202); // 90 x 90
define('USERINFO_AVATAR_170', 203); // 170 x 200


define('_USERINFO_EXTEND_START', 21);
define('USERINFO_INTERESTS', 21); // 兴趣爱好
define('USERINFO_IDOLS', 22); // 偶像
define('USERINFO_VISION', 23); // 近期愿景
define('USERINFO_MOTTO', 24); // 座右铭
define('USERINFO_FAV_MOVIES', 25); // 喜欢的影视
define('USERINFO_FAV_SPORTS', 26); // 喜欢的运动
define('USERINFO_FAV_BOOKS', 27); // 喜欢的书
define('USERINFO_FAV_MUSIC', 28); // 喜欢的音乐
define('USERINFO_QQ', 29); // QQ
define('USERINFO_MSN', 30); // MSN
define('USERINFO_EMAIL', 31); // 常用邮箱
define('USERINFO_MOBILE', 32); // 手机号码
define('USERINFO_PHONE', 33); // 固定电话
define('USERINFO_WEBSITE', 34); // 个人网站网址
define('USERINFO_ADDRESS', 35); // 住址
define('USERINFO_ZIPCODE', 36); // 邮编
define('_USERINFO_EXTEND_END', 36);




define('USERINFO_FEED_POINT', 50); // 动态触发点
define('USERINFO_ACCESS_AUTHORITY', 51); // 访问权限

// 学校字段详情
define('USERINFO_SCHOOL_ID', "id"); // 记录ID
define('USERINFO_SCHOOL_TYPE', "schoolType"); // 学校类型
define('USERINFO_SCHOOL_NAME', "schoolName"); // 学校名称
define('USERINFO_SCHOOL_COLLAGE', "major"); // 学校院系
define('USERINFO_SCHOOL_CLASS', "classroom"); // 班级
define('USERINFO_SCHOOL_START', "startYear"); // 入学时间



// 公司/机构详情
define('USERINFO_COMPANY_ID', "id"); // 公司/机构Id
define('USERINFO_COMPANY_NAME', "company"); // 公司/机构名称
define('USERINFO_COMPANY_DEPARTMENT', "position"); // 公司/机构部门
define('USERINFO_COMPANY_START', "startDate"); // 入职时间
define('USERINFO_COMPANY_END', "endDate"); // 离职时间

// 访问权限设置
define('_USERINFO_ACCESS_AUTHORITY_START', 0);
define('USERINFO_ACCESS_AUTHORITY_HOME', 0); // 谁可以浏览我的个人主页
define('USERINFO_ACCESS_AUTHORITY_MESSAGE_BOARD', 1); // 留言板
define('USERINFO_ACCESS_AUTHORITY_VISIT_FEED', 2); // 我的动态
define('USERINFO_ACCESS_AUTHORITY_VISIT_MOOD', 3); // 我的记录
define('USERINFO_ACCESS_AUTHORITY_INFO', 4); // 基本资料
define('USERINFO_ACCESS_AUTHORITY_CONTACTS', 5); // 联系方式
define('USERINFO_ACCESS_AUTHORITY_CONTACTS_QQ', 6); // QQ
define('USERINFO_ACCESS_AUTHORITY_CONTACTS_MSN', 7); // MSN
define('USERINFO_ACCESS_AUTHORITY_CONTACTS_EMAIL', 8); // 常用邮箱
define('USERINFO_ACCESS_AUTHORITY_CONTACTS_MOBILE', 9); // 手机号码
define('USERINFO_ACCESS_AUTHORITY_CONTACTS_PHONE', 10); // 固定电话
define('USERINFO_ACCESS_AUTHORITY_CONTACTS_WEBSITE', 11); // 个人网站网址
define('USERINFO_ACCESS_AUTHORITY_INTRESTS', 12); // 个人爱好
define('USERINFO_ACCESS_AUTHORITY_EDUCATION', 13); // 教育及工作信息
define('USERINFO_ACCESS_AUTHORITY_ADDRESS', 14); // 地址
define('_USERINFO_ACCESS_AUTHORITY_END', 14);



// 新鲜事触发点
define('_USERINFO_FEED_POINT_START', 14);
define('USERINFO_FEED_POINT_GET_MEDAL', 14); // 获得勋章
define('USERINFO_FEED_POINT_TWITTER', 15); // 推他
define('USERINFO_FEED_POINT_ADD_FRIEND', 16); // 添加好友
define('USERINFO_FEED_POINT_CHANGE_LOGO', 17); // 更新头像
define('_USERINFO_FEED_POINT_END', 17);


// 权限值定义
define('USERINFO_ACCESS_PUBLIC', 1); // 所有人访问
define('USERINFO_ACCESS_FRIEND', 2); // 好友可见
define('USERINFO_ACCESS_PRIVATE', 3); // 私有记录
define('USERINFO_ACCESS_SELF', 4); // 单独设置


// 完成度定义
define('USERINFO_COMPLETE_ALL', 0); // 所有信息完成度
define('USERINFO_COMPLETE_BASIC', 1); // 基本信息完成度
define('USERINFO_COMPLETE_CONTACTS', 2); // 联系方式完成度
define('USERINFO_COMPLETE_INTERESTS', 3); // 兴趣爱好完成度
define('USERINFO_COMPLETE_EDUCATION', 4); // 教育和公司完成度


define('USERINFO_CACHE_NAME', 'ucprofile'); //memcache分配的频道名
define('USERINFO_CACHE_EXPIRE', 1800); //memcache cache时间
define('USERINFO_ENABLE_CACHE', true); //启用memcache

/**
 * UCModUserInfo类用于查询/更新用户资料
 */
class UCModUserInfo {

	public static function checkNickName($user_id, $name) {
		$name = trim(str_replace('　', ' ', $name));
		$s_tmp = mb_convert_encoding($name, 'GBK', 'UTF-8');
		$len =  mb_strlen($s_tmp);
		if ($len<4) {
			return '保存失败，昵称最短为2个中文或4个字符';
		} else if ($len>14) {
			return '保存失败，昵称最多支持7个中文或14个字符';
		}

		if (preg_match('/(`|~|·|～|！|\!|@|#|￥|\$|%|…|…|\^|\&|\*|\(|\)|（|）|\-|—|—|\+|=|\{|\}|\[|\]|『|』|【|】|\||\\、|;|；|:|\：|“|‘|\'|\"|<|>|《|》|,|\.|，|。|\/|\?|？| |　)/', $name)) {
			return '保存失败，包含禁止特殊字符';
		}
		
		require_once(dirname(__FILE__).'/../../libraries/UCLibMISApi.php');
		$ret = UCLibMISApi::check($user_id, UC_MIS_CHANNEL_NICKNAME, $name);

		if(UC_MIS_RESULT_BAD == $ret) {
			return '保存失败，'.$name.'过于有性格';
		} else if(UC_MIS_RESULT_FREQ_LIMIT == $ret) {
			return '保存失败，您操作过于频繁';
		}
		return null;
	}

	/**
	 * 获取单个用户信息
	 * @param user_id: 用户id
	 * @param attributes: 所需要获取的用户属性列表
	 *    比如 array(USERINFO_NICKNAME,
	 *                     USERINFO_GENDER_DESC,
	 *                     USERINFO_BIRTH_DAY,
	 *                     USERINFO_BLOOD_DESC);
	 * @return: 用户信息
	 *    比如 array(USERINFO_NICKNAME => "张三",
	 *                     USERINFO_GENDER_DESC => "男",
	 *                     USERINFO_BIRTH_DAY => "1981-01-26",
	 *                     USERINFO_BLOOD_DESC => "A型");
	 */
	public static function getUserInfo($user_id, $attributes, $public_only = false) {
		$user = self::getUser($user_id);
		return $user->getAttributes($attributes, $public_only);
	}

	/**
	 * 检查用户id是否存在
	 * @param user_id: 用户id
	 * @return 1: 存在 0: 不存在
	 */
	public static function isUCUser($user_id) {
		UCLibLogger::info("check uc user:".$user_id);
		$user = self::getUserInfo($user_id, array(USERINFO_NICKNAME, USERINFO_ACTIVE_FLAG));
		if ($user === false)  {
			self::$_error_no = 2;
			return false;
		}
		if ($user && isset($user[USERINFO_ACTIVE_FLAG]) && $user[USERINFO_ACTIVE_FLAG] == 1 && isset($user[USERINFO_NICKNAME]) && $user[USERINFO_NICKNAME]) {
			return 1;
		} else {
			return 0;
		}
	}


	/**
	 * 根据用户昵称返回sdid
	 * @param name:  用户昵称
	 * @return 用户id, false：不存在
	 */
	public static function getSdidByName($name) {
		$result = SDOApiPeopleService::getSdid($name);
		if (SDOSocket::isError($result) || $result["code"] != 0)  {
			self::$_error_no = 2;
			return false;
		}
		$ret = SDOApiHelper::getOneResult($result, null);
		return $ret ? $ret : 0;
	}

	/**
	 * 获取批量用户信息
	 * @param user_ids: 用户id列表，比如
	 *    比如 array(123, 456, 789)
	 * @param attributes: 所需要获取的用户属性列表
	 *    比如 array(USERINFO_NICKNAME,
	 *                     USERINFO_GENDER_DESC,
	 *                     USERINFO_BIRTH_DAY,
	 *                     USERINFO_BLOOD_DESC);
	 * @return: 用户信息
	 *    比如 array (
	 *                123 => array(USERINFO_NICKNAME => "张三",
	 *                     USERINFO_GENDER_DESC => "男",
	 *                     USERINFO_BIRTH_DAY => "1981-01-26",
	 *                     USERINFO_BLOOD_DESC => "A型"),
	 *                456 => array(USERINFO_NICKNAME => "张三",
	 *                     USERINFO_GENDER_DESC => "男",
	 *                     USERINFO_BIRTH_DAY => "1981-01-26",
	 *                     USERINFO_BLOOD_DESC => "A型"),
	 *                789 => array(USERINFO_NICKNAME => "张三",
	 *                     USERINFO_GENDER_DESC => "男",
	 *                     USERINFO_BIRTH_DAY => "1981-01-26",
	 *                     USERINFO_BLOOD_DESC => "A型"))
	 */
	public static function getUserInfos($user_ids, $attributes, $public_only = false) {
		$users = self::getUsers($user_ids);

		$has_basic = false;
		$has_extend = false;

		foreach ($attributes as $key) {
			if ($key == USERINFO_GENDER_DESC
				|| $key == USERINFO_BLOOD_DESC
				|| $key == USERINFO_NATIONALITY_DESC
				|| $key == USERINFO_LOCATION_DESC
				|| $key == USERINFO_HOMETOWN_DESC
				|| $key == USERINFO_HOMETOWN
				|| $key == USERINFO_AVATAR_30
				|| $key == USERINFO_AVATAR_45
				|| $key == USERINFO_AVATAR_90
				|| $key == USERINFO_AVATAR_170
				|| ($key >= _USERINFO_BASIC_START && $key <= _USERINFO_BASIC_END)) {
				$has_basic = true;
			} else if ($key >= _USERINFO_EXTEND_START && $key <= _USERINFO_EXTEND_END) {
				$has_extend = true;
			} else {
				self::$_error_no = 1;
				return false;
			}
		}
		
		$cache = null;
		if (defined("USERINFO_ENABLE_CACHE")) {
			// read from memcache
			$keys = array();
			if ($has_basic) {
				foreach ($users as $user_id => &$user) {
					if ($user->_user_basic_info === null) {
						$keys []= $user->_user_id.".basic";
					}
					if ($user->_user_extend_info === null) {
						$keys []= $user->_user_id.".extend";
					}
				}
			}

			if ($keys) {
				if ($cache === null) $cache = new UCLibCache(USERINFO_CACHE_NAME, false);
				$result = $cache->read_multi($keys);

				foreach($result as $k => &$v) {
					$key = substr($k, strlen(USERINFO_CACHE_NAME) + 1);
					$index = strpos($key, ".");
					if ($index) {
						$sdid = substr($key, 0, $index);
						if (!isset($users[$sdid])) continue;
						$type = substr($key, $index + 1);
						if ($type == "basic") {
							$users[$sdid]->_user_basic_info = json_decode($v, true);
						} else if ($type == "extend") {
							$users[$sdid]->_user_extend_info = json_decode($v, true);
						}
					}
				}
			}
		} 

		if ($has_basic) {
			$ids = array();
			foreach ($users as $user_id => &$user) {
				if ($user->_user_basic_info === null) {
					$ids []= $user_id;
				}
			}
			if ($ids) {
				$result = SDOApiPeopleService::getMultiBasicInfo(join(",", $ids));
				if (SDOSocket::isError($result) || $result["code"] != 0)  {
					self::$_error_no = 2;
					return false;
				}
				$entities = SDOApiHelper::getArrayResult($result);
				foreach ($ids as $id) {
					$entry = null;
					foreach ($entities as $entity) {
						$sdid = $entity["sdid"];
						if ($sdid == $id) {
							$entry = $entity;
							break;
						}
					}
					$users[$id]->_fillBasicInfo($entry);

					if (defined("USERINFO_ENABLE_CACHE")) {
						if ($cache === null) $cache = new UCLibCache(USERINFO_CACHE_NAME, false);
						$cache->write($id.".basic", json_encode($users[$id]->_user_basic_info), USERINFO_CACHE_EXPIRE);
					} 
				}
			}
		}
		if ($has_extend) {
			$ids = array();
			foreach ($users as $user_id => &$user) {
				if ($user->_user_extend_info === null) {
					$ids []= $user_id;
				}
			}
			if ($ids) {
				$result = SDOApiPeopleService::getMultiExtendInfo(join(",", $ids));
				if (SDOSocket::isError($result) || $result["code"] != 0)  {
					self::$_error_no = 2;
					return false;
				}
				$entities = SDOApiHelper::getArrayResult($result);
				foreach ($ids as $id) {
					$entry = null;
					foreach ($entities as $entity) {
						$sdid = $entity["sdid"];
						if ($sdid == $id) {
							$entry = $entity;
							break;
						}
					}
					$users[$id]->_fillExtendInfo($entry);
					if (defined("USERINFO_ENABLE_CACHE")) {
						if ($cache === null) $cache = new UCLibCache(USERINFO_CACHE_NAME, false);
						$cache->write($id.".extend", json_encode($users[$id]->_user_extend_info), USERINFO_CACHE_EXPIRE);
					} 
				}
			}
		}

		$result = array();
		foreach ($users as $user_id => &$user) {
			$info =  $user->getAttributes($attributes, $public_only);
			if ($info === false) return false;
			$result[$user_id] = $info;
		}
		if (defined("USERINFO_ENABLE_CACHE")) {
			if ($cache !== null) $cache->close();
		} 
		return $result;
	}

	/**
	 * 获取UserInfo实例
	 * @param user_id: 用户id
	 * @return: UserInfo实例
	 */
	public static function getUser($user_id) {
		if (isset(self::$_cached_users[$user_id])) return self::$_cached_users[$user_id];
		$userinfo = new UCModUserInfo($user_id);
		self::$_cached_users[$user_id] = $userinfo;
		return $userinfo;
	}
	
	/**
	 * 批量获取UserInfo实例
	 * @param user_ids: array of 用户id
	 * @return: UserInfo实例数组，以用户id为key
	 */
	public static function getUsers($user_ids) {
		$result = array();
		foreach ($user_ids as $user_id) {
			$result[$user_id] = self::getUser($user_id);
		}
		return $result;
	}
	
	/**
	 * 返回用户的头像
	 * @param user_id: 用户id
	 * @param version: 用户版本号
	 * @param size: 尺寸头像
	 * @return: 头像的url
	 */
	public static function getUserAvatar($user_id, $version, $size) {
		
		if (!$version) {
			$prefix = UC_CONF_STATIC_DOMAIN."/images/avatar/";
			$gender = self::getUser($user_id)->getValue(USERINFO_GENDER);
			switch($gender) {
				case 1:
					$prefix .= "boy_";
					break;
				case 2:
					$prefix .= "girl_";
					break;
				default:
					$prefix .= "default_";
					break;
			}
			if ($size == USERINFO_AVATAR_30) {
				return $prefix."30x30.gif";
			} else if ($size == USERINFO_AVATAR_45) {
				return $prefix."45x45.gif";
			} else if ($size == USERINFO_AVATAR_90) {
				return $prefix."90x90.gif";
			} else if ($size == USERINFO_AVATAR_170) {
				return $prefix."170x200.gif";
			} else  {
				return false;
			}			
		}

		$str = md5($user_id);
		$prefix = '/' . substr($str, 0, 2) . '/' . substr($str, 2, 2) . '/' . $user_id . '/';
				
		$file = "";
		if ($size == USERINFO_AVATAR_30) {
			$file = $prefix ."30x30.jpg";
		} else if ($size == USERINFO_AVATAR_45) {
			$file = $prefix ."45x45.jpg";
		} else if ($size == USERINFO_AVATAR_90) {
			$file = $prefix ."90x90.jpg";
		} else if ($size == USERINFO_AVATAR_170) {
			$file = $prefix ."170x200.jpg";
		} else  {
			self::$_error_no = 1;
			return false;
		}
	
		//$user_id = $user_id . "0000";
		//$dir = substr($user_id, 0, 2). "/" . substr($user_id, 2, 2);
		return UC_CONF_FACE_DOMAIN.$file."?$version";
	}


	/**
	 * 获取最后的错误代码
	 * @return 错误代码
	 *  具体含义请看 http://aqyw.sdo.com/projects/uc/wiki/UC%E9%94%99%E8%AF%AF%E4%BB%A3%E7%A0%81%E8%A1%A8
	 */
	public static function getLastErrorNo() {
		return self::$_error_no;
	}

	//
	// 以下为具体实现
	//

	private $_user_id;
	private $_user_basic_info = null;
	private $_user_extend_info = null;
	private $_user_school = null;
	private $_user_company = null;
	private $_user_access = null;
	private $_user_blacklist = null;
	private $_user_relation = null;
	private $_cached_blacklist = array();
	private $_cached_relation_types = array();

	private static $_error_no = null;
	private static $_cached_users = array();

	/**
	 * 构造函数
	 * @param user_id: 用户id
	 * 注意：请不要直接调用这个方法构造，而是通过getUser静态方法
	 */
	protected function __construct($user_id) {
		//	if (!$user_id) throw new Exception("Invalid user id".$user_id);
		$this->_user_id = $user_id;
	}

	
	/**
	 * 获取用户昵称
	 */
	public function getName() {
		return $this->getValue(USERINFO_NICKNAME);
	}

	/**
	 * 获取用户昵称
	 */
	public function getAvatar($size) {
		return UCModUserInfo::getUserAvatar($this->_user_id, $this->getValue(USERINFO_AVATAR_VERSION), $size);
	}

	/**
	 * 按照字段名返回用户的基本资料
	 * @param key: 用户资料类型
	 * @param public_only: 只返回公开的资料，默认为false
	 */
	public function getValue($key, $public_only = false) {
		if ($key == USERINFO_GENDER_DESC) { // 性别描述
			$value = $this->getValue(USERINFO_GENDER);
			if ($value === false) return false;
			return UCLibCodes::$genders->getName($value);
		} else if ($key == USERINFO_BLOOD_DESC) { // 血型描述
			$value = $this->getValue(USERINFO_BLOOD);
			if ($value === false) return false;
			return UCLibCodes::$blood_groups->getName($value);
		} else if ($key == USERINFO_NATIONALITY_DESC) { // 民族描述
			$value = $this->getValue(USERINFO_NATIONALITY);
			if ($value === false) return false;
			return UCLibCodes::$nationalities->getName($value);
		} else if ($key == USERINFO_LOCATION_DESC) { // 地点描述
			$value = $this->getValue(USERINFO_LOCATION);
			if ($value === false) return false;
			return UCLibLocation::getFullName($value);
		} else if ($key == USERINFO_HOMETOWN_DESC) { // 出生地描述
			$value = $this->getValue(USERINFO_HOMETOWN);
			if ($value === false) return false;
			return UCLibLocation::getFullName($value);
		} else if ($key == USERINFO_AVATAR_30 || $key == USERINFO_AVATAR_45 || $key == USERINFO_AVATAR_90 || $key == USERINFO_AVATAR_170) { // 头像
			$value = $this->getValue(USERINFO_AVATAR_VERSION);
			if ($value === false) return false;
			return self::getUserAvatar($this->_user_id, $value, $key);
		}

		if (!$this->_checkType($key)) return false;

		if ($key >= _USERINFO_BASIC_START && $key <= _USERINFO_BASIC_END) {
			if (!$this->_checkBasicInfo()) return false;
			return isset($this->_user_basic_info[$key]) ? $this->_user_basic_info[$key] : "";
		} else {
			if (!$this->_checkExtendInfo()) return false;
			$result = isset($this->_user_extend_info[$key]) ? $this->_user_extend_info[$key] : "";
			if (!$result || $public_only === false) return $result;

			switch ($key) {
				case USERINFO_INTERESTS:
				case USERINFO_IDOLS:
				case USERINFO_VISION:
				case USERINFO_MOTTO:
				case USERINFO_FAV_MOVIES:
				case USERINFO_FAV_SPORTS:
				case USERINFO_FAV_BOOKS:
				case USERINFO_FAV_MUSIC:
					return $this->getAccessAuthority(USERINFO_ACCESS_AUTHORITY_INTRESTS) == USERINFO_ACCESS_PUBLIC ? $result : "";
				case USERINFO_QQ:
					return $this->getAccessAuthority(USERINFO_ACCESS_AUTHORITY_CONTACTS_QQ) == USERINFO_ACCESS_PUBLIC ? $result : "";
				case USERINFO_MSN:
					return $this->getAccessAuthority(USERINFO_ACCESS_AUTHORITY_CONTACTS_MSN) == USERINFO_ACCESS_PUBLIC ? $result : "";
				case USERINFO_EMAIL:
					return $this->getAccessAuthority(USERINFO_ACCESS_AUTHORITY_CONTACTS_EMAIL) == USERINFO_ACCESS_PUBLIC ? $result : "";
				case USERINFO_MOBILE:
					return $this->getAccessAuthority(USERINFO_ACCESS_AUTHORITY_CONTACTS_MOBILE) == USERINFO_ACCESS_PUBLIC ? $result : "";
				case USERINFO_PHONE:
					return $this->getAccessAuthority(USERINFO_ACCESS_AUTHORITY_CONTACTS_PHONE) == USERINFO_ACCESS_PUBLIC ? $result : "";
				case USERINFO_WEBSITE:
					return $this->getAccessAuthority(USERINFO_ACCESS_AUTHORITY_CONTACTS_WEBSITE) == USERINFO_ACCESS_PUBLIC ? $result : "";
				case USERINFO_ADDRESS:
					return $this->getAccessAuthority(USERINFO_ACCESS_AUTHORITY_ADDRESS) == USERINFO_ACCESS_PUBLIC ? $result : "";
				case USERINFO_ZIPCODE:
					return $this->getAccessAuthority(USERINFO_ACCESS_AUTHORITY_ADDRESS) == USERINFO_ACCESS_PUBLIC ? $result : "";
				default: return $result;
			}
		}
	}

	/**
	 * 按照多个字段名返回用户的属性列表，支持多参数，比如
	 * getValues(USERINFO_NICKNAME, USERINFO_GENDER);
	 * @return: 用户资料数组，比如
	 *    array(USERINFO_NICKNAME => "张三", USERINFO_GENDER => 1);
	 */	 
	public function getValues() {
		 echo "<b>你调用了UCModUserInfo::getValues建议改用UCModUserInfo::getAttributes方法</b>";
		$arg_list = func_get_args();
		$result = array();
        foreach ($arg_list as $arg) {
			$value = $this->getValue($arg);
			if ($value === false) return false;
			$result[$arg] = $value;
        }
		return $result;
	}

	
	/**
	 * 按照多个字段名返回用户的属性列表
	 * getAttributes(array(USERINFO_NICKNAME, USERINFO_GENDER));
	 * @param attributes: 属性列表
	 *   比如array(USERINFO_NICKNAME, USERINFO_GENDER)
	 * @return: 用户资料数组，比如
	 *    array(USERINFO_NICKNAME => "张三", USERINFO_GENDER => 1);
	 */	 
	public function getAttributes($attributes, $public_only = false) {
		$result = array();
        foreach ($attributes as $arg) {
			$value = $this->getValue($arg, $public_only);
			if ($value === false) return false;
			$result[$arg] = $value;
        }
		return $result;
	}

	/**
	 * 检查用户的行为是否生成新鲜事
	 * @param type: 事件类型
	 * @return 1:生成, 0:不生成
	 */
	public function checkFeedPoint($type) {
		if ($this->_checkAccess() === false) return false;

		$length = strlen($this->_user_access);
		if ($type >= $length) return 1;

		return intval(substr($this->_user_access, $type, 1));
	}

	/**
	 * 返回新鲜事的生成点
	 */
	public function getFeedPoints() {
		$medal = $this->checkFeedPoint(USERINFO_FEED_POINT_GET_MEDAL);
		if ($medal === false) return false;
		$twitter = $this->checkFeedPoint(USERINFO_FEED_POINT_TWITTER);
		if ($twitter === false) return false;
		$addfriend = $this->checkFeedPoint(USERINFO_FEED_POINT_ADD_FRIEND);
		if ($addfriend === false) return false;
		$logo = $this->checkFeedPoint(USERINFO_FEED_POINT_CHANGE_LOGO);
		if ($logo === false) return false;
		return array(
			USERINFO_FEED_POINT_GET_MEDAL => $medal,
			USERINFO_FEED_POINT_TWITTER => $twitter,
			USERINFO_FEED_POINT_ADD_FRIEND => $addfriend,
			USERINFO_FEED_POINT_CHANGE_LOGO => $logo);
	}

	/**
	 * 设置用户的行为是否生成新鲜事
	 * @param points: 需要生成的新鲜事类型，为一个集合，比如
	 *    array(USERINFO_FEED_POINT_GET_MEDAL, USERINFO_FEED_POINT_POST);
	 *    可选项为
     *         USERINFO_FEED_POINT_GET_MEDAL  // 获得勋章
     *         USERINFO_FEED_POINT_TWITTER // 推他
     *         USERINFO_FEED_POINT_ADD_FRIEND // 添加好友
     *         USERINFO_FEED_POINT_CHANGE_LOGO // 更新头像
	 */
	public function setFeedPoints($points) {
		if ($this->_checkAccess() === false) return false;

		$length = strlen($this->_user_access);
		foreach ($points as $k => $v) {
			if ($k < _USERINFO_FEED_POINT_START || $k > _USERINFO_FEED_POINT_END) {
				self::$_error_no = 1;
				return false;
			}
			if ($v != 1 && $v != 0) {
				self::$_error_no = 1;
				return false;
			}

			while ($length <= $k) {
				$this->_user_access .= 1;
				$length += 1;
			}
			$this->_user_access[$k] = $v;
		}

		$result = SDOApiUCProfilePrivilegeService::updateProfilePrivilege($this->_user_id, $this->_user_access);
		if (SDOSocket::isError($result))  {
			self::$_error_no = 2;
			return false;
		}
		return true;
	}

	/**
	 * 检查访问权限
	 * @param type: 访问类型
	 * @return: 返回类型，为下面一种
     *        USERINFO_ACCESS_PUBLIC // 所有人访问
     *        USERINFO_ACCESS_FRIEND // 好友可见
     *        USERINFO_ACCESS_PRIVATE // 私有记录
	 */
	public function getAccessAuthority($type) {
		if ($this->_checkAccess() === false) return false;
		$length = strlen($this->_user_access);
		if ($type >= $length) return USERINFO_ACCESS_PUBLIC;

		return intval(substr($this->_user_access, $type, 1));
	}

	/**
	 * 设置访问权限
	 * @param auths: 权限的集合，比如
	 *    array(USERINFO_ACCESS_AUTHORITY_HOME => USERINFO_ACCESS_PUBLIC,
	 *             USERINFO_ACCESS_AUTHORITY_CONTACTS_QQ => USERINFO_ACCESS_FRIEND);
	 *    key的可选范围请查看USERINFO_ACCESS_AUTHORITY_XXX，value察看USERINFO_ACCESS_XXX定义
	 */
	public function setAccessAuthorities($auths) {
		if ($this->_checkAccess() === false) return false;

		$length = strlen($this->_user_access);
		foreach ($auths as $k => $v) {
			if ($k < _USERINFO_ACCESS_AUTHORITY_START || $k > _USERINFO_ACCESS_AUTHORITY_END) {
				self::$_error_no = 1;
				return false;
			}
			if ($k === USERINFO_ACCESS_AUTHORITY_CONTACTS) {
				if ($v != USERINFO_ACCESS_PUBLIC && $v != USERINFO_ACCESS_PRIVATE && $v != USERINFO_ACCESS_FRIEND && $v != USERINFO_ACCESS_SELF) {
					self::$_error_no = 1;
					return false;
				}
			} else {
				if ($v != USERINFO_ACCESS_PUBLIC && $v != USERINFO_ACCESS_PRIVATE && $v != USERINFO_ACCESS_FRIEND) {
					self::$_error_no = 1;
					return false;
				}
			}

			while ($length <= $k) {
				$this->_user_access .= USERINFO_ACCESS_PUBLIC;
				$length += 1;
			}
			$this->_user_access[$k] = $v;
		}

		$result = SDOApiUCProfilePrivilegeService::updateProfilePrivilege($this->_user_id, $this->_user_access);
		if (SDOSocket::isError($result))  {
			self::$_error_no = 2;
			return false;
		}
		return true;
	}

	/**
	 * 检查某个用户是否可以访问某些类型的信息
	 * @param user_id: 需要检查的用户的user_id
	 * @param type: 访问类型，见USERINFO_ACCESS_AUTHORITY_XXXX定义
	 * @return 1:是, 0:不是
	 */
	public function isAccessable($user_id, $type) {
		if ($user_id == $this->_user_id) return 1;
		$ret = $this->isInMyBlackList($user_id);
		if ($ret === false) {
			return false;
		}
		if ($ret == 1) {
			return 0; // 黑名单不允许进行任何访问
		}

		$access_authority = $this->getAccessAuthority($type);
		if ($access_authority === false) return false;

		if ($access_authority == USERINFO_ACCESS_PRIVATE ||
		   ($access_authority == USERINFO_ACCESS_FRIEND && !$this->isMyFirend($user_id))) {
			return 0; // 用户设置为私有或者好友可见
		}
		return 1; // 通过检查
	}


	/**
	 * 返回某个用户的访问权限
	 * @param user_id: 需要检查的用户的user_id
	 * @return array: key是USERINFO_ACCESS_AUTHORITY_XXXX定义，value：1可以访问
	 */
	public function getFullAccess($user_id) {
		$retult = array();
		if ($user_id == $this->_user_id) {
			for ($i = _USERINFO_ACCESS_AUTHORITY_START; $i <= _USERINFO_ACCESS_AUTHORITY_END; $i += 1) {
				$result[$i] = 1;
			}
			return $result;
		}
		$ret = $this->isInMyBlackList($user_id);
		if ($ret === false) {
			return false;
		}
		if ($ret == 1) {
			for ($i = _USERINFO_ACCESS_AUTHORITY_START; $i <= _USERINFO_ACCESS_AUTHORITY_END; ++$i)
				$result[$i] = 0;
			return $result;
		}

		for ($i = _USERINFO_ACCESS_AUTHORITY_START; $i <= _USERINFO_ACCESS_AUTHORITY_END; ++$i) {
			$access_authority = $this->getAccessAuthority($i);
			if ($access_authority === false) return false;

			if ($access_authority == USERINFO_ACCESS_PRIVATE ||
			   ($access_authority == USERINFO_ACCESS_FRIEND && !$this->isMyFirend($user_id))) {
				$result[$i] = 0; // 用户设置为私有或者好友可见
			}
			$result[$i] = 1; // 通过检查
		}
		return $result;
	}


	/**
	 * 更新用户的资料
	 * @param values: 需要增加的 键=>值 对，比如
	 *     array(USERINFO_QQ => "123456",
	 *              USERINFO_MSN => "xxxx@snda.com")
	 *     注意公司/学校信息请直接调用addSchool和addCompany函数
	 */
	public function update($values) {
		$basic_dirty = false;
		$extend_dirty = false;

		$basic = array();
		$extend = array();

		foreach ($values as $k => $v) {
			if ($k >= _USERINFO_BASIC_START && $k <= _USERINFO_BASIC_END) {
				$basic[$k] = $v;
				if ($this->_user_basic_info != null) $this->_user_basic_info[$k] = $v;
				$basic_dirty = true;
			} else if ($k >= _USERINFO_EXTEND_START && $k <= _USERINFO_EXTEND_END) {
				$extend[$k] = $v;
				if ($this->_user_extend_info != null) $this->_user_extend_info[$k] = $v;
				$extend_dirty = true;
			}
		}

		if ($basic_dirty) {
			if (!$this->_saveBasicInfo($basic)) return false;
		}
		if ($extend_dirty) {
			if (!$this->_saveExtendInfo($extend)) return false;
		}
		return true;
	}

	/**
	 * 获取所有学校信息
	 * @return: 学校列表，每一个成员如下
	 *     array(USERINFO_SCHOOL_ID => 123,
	 *              USERINFO_SCHOOL_TYPE => 0,
	 *              USERINFO_SCHOOL_NAME => "清华大学",
	 *              USERINFO_SCHOOL_COLLAGE => "计算机系",
	 *              USERINFO_SCHOOL_CLASS => "3班",
	 *              USERINFO_SCHOOL_START => "1981") // 入学时间
	 */
	public function getSchoolList() {
		if (!$this->_checkSchool()) return false;
		return $this->_user_school;
	}


	/**
	 * 增加一个学校信息
	 * @param school: 需要增加的学校信息，必须包含
	 *     array(USERINFO_SCHOOL_TYPE => 0,
	 *              USERINFO_SCHOOL_NAME => "清华大学",
	 *              USERINFO_SCHOOL_COLLAGE => "计算机系",
	 *              USERINFO_SCHOOL_CLASS => "3班",
	 *              USERINFO_SCHOOL_START => "1981") // 入学时间
	 * @return: 新加学校的id
	 */
	public function addSchool($school) {
		$result = SDOApiPeopleService::addEducation($this->_user_id, $school);
		if (SDOSocket::isError($result)) {
			return false;
		} else {
			$id = $result["data"]["data"];
			if ($this->_user_school != null) { // 添加到当前数据中
				$school[USERINFO_SCHOOL_ID] = $id;
				$this->_user_school []= $school;
			}
			return $id;
		}
	}

	/**
	 * 删除一个学校信息
	 * @param school_id: 需要删除的学校id
	 */
	public function deleteSchool($school_id) {
		$result = SDOApiPeopleService::deleteEducation($this->_user_id, $school_id);
		if (SDOSocket::isError($result)) {
			return false;
		} else {
			if ($this->_user_school != null) { //从当前数据中删除
				foreach ($this->_user_school as $k => &$v) {
					if ($v[USERINFO_SCHOOL_ID] == $school_id) {
						unset($this->_user_school[$k]);
						break;
					}
				}
			}
			return true;
		}
	}

	/**
	 * 获取所有公司信息
	 * @return: 公司列表，每一个成员如下
	 *     array(USERINFO_COMPANY_ID =>123,
	 *              USERINFO_COMPANY_TYPE => 0,
	 *              USERINFO_COMPANY_NAME => "盛大",
	 *              USERINFO_COMPANY_DEPARTMENT => "平台技术部",
	 *              USERINFO_COMPANY_START => "2008") // 入职时间
	 *              USERINFO_COMPANY_END => "") // 离职时间，如果为现在，设置为""
	 */
	public function getCompanyList() {
		if (!$this->_checkCompany()) return false;
		return $this->_user_company;
	}

	/**
	 * 增加一个公司信息
	 * @param company: 需要增加的公司信息，必须包含
	 *     array(USERINFO_COMPANY_TYPE => 0,
	 *              USERINFO_COMPANY_NAME => "盛大",
	 *              USERINFO_COMPANY_DEPARTMENT => "平台技术部",
	 *              USERINFO_COMPANY_START => "2008-12") // 入职时间
	 *              USERINFO_COMPANY_END => "") // 离职时间，如果为现在，设置为""
	 * @return: 新加公司的id
	 */ 
	public function addCompany($company) {
		if (isset($company[USERINFO_COMPANY_START])) {
			$company[USERINFO_COMPANY_START] .= "-1";
		}
		if (isset($company[USERINFO_COMPANY_END]) && $company[USERINFO_COMPANY_END]) {
			$company[USERINFO_COMPANY_END] .= "-1";
		}
		$result = SDOApiPeopleService::addCompany($this->_user_id, $company);
		if (SDOSocket::isError($result)) {
			return false;
		} else {	
			$id = $result["data"]["data"];
			if ($this->_user_company != null) { // 添加到当前数据中
				$company[USERINFO_COMPANY_ID] = $id;
				$this->_user_company []= $company;
			}
			return $id;
		}
	}

	/**
	 * 删除一个公司信息
	 * @param company_id: 需要删除的公司id
	 */
	public function deleteCompany($company_id) {
		$result = SDOApiPeopleService::deleteCompany($this->_user_id, $company_id);
		if (SDOSocket::isError($result)) {
			return false;
		} else {
			if ($this->_user_company != null) { //从当前数据中删除
				foreach ($this->_user_company as $k => &$v) {
					if ($v[USERINFO_COMPANY_ID] == $company_id) {
						unset($this->_user_company[$k]);
						break;
					}
				}
			}
			return true;
		}
	}


	private function _calcComplete($value, $keys) {
		if (!$value) return 0;
		$cnt = 0;
		foreach($keys as $key) {
			if (isset($value[$key]) && $value[$key]) $cnt +=1;
		}
		return $this->_roundComplete($cnt / count($keys));
	}

	private function _roundComplete($v) {
		if ($v > 0 && $v < 0.2) return 20;
		if ($v < 1 && $v > 0.8) return 80;
		return intval(5 * ($v+ 0.1)) * 20;
	}
	/**
	 * 获得信息完成度
	 * @param type: 信息类型，默认为USERINFO_COMPLETE_ALL(所有信息)
	 * @return 返回完成度的百分比的值，比如80%返回为80
	 */
	public function getCompletePercent($type = USERINFO_COMPLETE_ALL) {
		if ($type == USERINFO_COMPLETE_ALL) {
			return $this->_roundComplete(($this->getCompletePercent(USERINFO_COMPLETE_BASIC) + 
						$this->getCompletePercent(USERINFO_COMPLETE_CONTACTS) +
						$this->getCompletePercent(USERINFO_COMPLETE_INTERESTS) +
						$this->getCompletePercent(USERINFO_COMPLETE_EDUCATION)) / 400);
		}

		if ($type == USERINFO_COMPLETE_BASIC) {
			$this->_checkBasicInfo();
			return $this->_calcComplete($this->_user_basic_info, array(
					USERINFO_NICKNAME,
					//USERINFO_AVATAR_VERSION,
					USERINFO_GENDER,
					USERINFO_BIRTH_DAY,
					USERINFO_BLOOD,
					USERINFO_NATIONALITY,
					USERINFO_LOCATION,
					USERINFO_HOMETOWN,
					USERINFO_KEYWORDS));
		} else if ($type == USERINFO_COMPLETE_CONTACTS) {
			$this->_checkExtendInfo();
			return $this->_calcComplete($this->_user_extend_info, array(
					USERINFO_QQ,
					USERINFO_MSN,
					USERINFO_EMAIL,
					USERINFO_MOBILE,
					USERINFO_PHONE,
					USERINFO_WEBSITE,
					USERINFO_ADDRESS,
					USERINFO_ZIPCODE));
		} else if ($type == USERINFO_COMPLETE_INTERESTS) {
			$this->_checkExtendInfo();
			return $this->_calcComplete($this->_user_extend_info, array(
					USERINFO_INTERESTS,
					USERINFO_IDOLS,
					USERINFO_VISION,
					USERINFO_MOTTO,
					USERINFO_FAV_MOVIES,
					USERINFO_FAV_SPORTS,
					USERINFO_FAV_BOOKS,
					USERINFO_FAV_MUSIC));
		} else if ($type == USERINFO_COMPLETE_EDUCATION) {
			$this->_checkSchool();
			$this->_checkCompany();

			$cnt = 0;
			if ($this->_user_company) $cnt +=1;
			$codes = UCLibCodes::$action_types->getList();
			if ($this->_user_school) {
				foreach ($codes as $key => &$v) {
					foreach ($this->_user_school as &$school) {
						if ($school[USERINFO_SCHOOL_TYPE] == $key) {
							$cnt += 1;
							break;
						}
					}
				}
			}
			return $this->_roundComplete($cnt / (count($codes)));
		} else {
			return 0;
		}
	}

	/**
	 * 获得信息完成项目数
	 */
	public function getItems() {
		$items = 0;
		if (false === $this->_checkBasicInfo()) return false;
		foreach ($this->_user_basic_info as $v) {
			if ($v) $items += 1;
		}
		if (false === $this->_checkExtendInfo()) return false;
		foreach ($this->_user_extend_info as $v) {
			if ($v) $items += 1;
		}
		if (false === $this->_checkSchool()) return false;
		if (false === $this->_checkCompany()) return false;
		$items += count($this->_user_school);
		$items += count($this->_user_company);
		return $items;
	}
	/**
	 * 检查某个用户是否在黑名单列表中
	 * @param user_id: 需要检查的用户的user_id
	 * @return 1:是, 0:不是
	 */
	public function isInMyBlackList($user_id) {
		if (isset($this->_cached_blacklist[$user_id])) return $this->_cached_blacklist[$user_id];

		if ($this->_user_blacklist === null) {
			$this->_user_blacklist = new UCModBlackList($this->_user_id);
		}
		$ret = $this->_user_blacklist->getJudgePerson($user_id);
		$this->_cached_blacklist[$user_id] = $ret;
		return $ret;
	}

	/**
	 * 检查某个用户是否为我的好友
	 * @param user_id: 需要检查的用户的user_id
	 * @return true:是, false:不是
	 */
	public function isMyFirend($user_id) {
		if (isset($this->_cached_relation_types[$user_id])) return $this->_cached_relation_types[$user_id];
		
		if ($this->_user_relation === null) {
			$this->_user_relation = new UCModRelation($this->_user_id);
		}
		$result = $this->_user_relation->getRelationType(array($user_id), UCConfRelation::RELATION_TYPE_FOLLOW);
		$ret = 0;
		if (isset($result[$user_id]) && $result[$user_id] == 1) $ret = 1;
		$this->_cached_relation_types[$user_id] = $ret;
		return $ret;

	}
	
	/**
	 * 检查字段名是否合法
	 * @param type: 字段类型
	 */
	private function _checkType($type) {
		if ((gettype($type) == "integer" && 
			(($type >= _USERINFO_BASIC_START && $type <= _USERINFO_BASIC_END) ||
			  ($type >= _USERINFO_EXTEND_START && $type <= _USERINFO_EXTEND_END)))) {
			return true;
		} else {
			self::$_error_no = 1;
			return false;
		}
	}

	
	 function _checkAccess() {
		if ($this->_user_access === null) {
			$result = SDOApiUCProfilePrivilegeService::getProfilePrivilege($this->_user_id);
			// todo: check code;
			if (SDOSocket::isError($result) || $result["code"] != 0)  {
				self::$_error_no = 2;
				return false;
			}
			$this->_user_access = SDOApiHelper::getOneResult($result, "");
		}
		return true;
	}

	private function _checkSchool() {
		if ($this->_user_school === null) {
			$result = SDOApiPeopleService::getEducations($this->_user_id);
			if (SDOSocket::isError($result) || $result["code"] != 0)  {
				self::$_error_no = 2;
				return false;
			}
			$this->_user_school = SDOApiHelper::getArrayResult($result);
		}
		return true;
	}

	private function _checkCompany() {
		if ($this->_user_company === null) {
			$result = SDOApiPeopleService::getCompanys($this->_user_id);
			if (SDOSocket::isError($result) || $result["code"] != 0)  {
				self::$_error_no = 2;
				return false;
			}
			
			$this->_user_company = SDOApiHelper::getArrayResult($result);
			foreach($this->_user_company as &$company) {
				if (isset($company[USERINFO_COMPANY_START]) && $company[USERINFO_COMPANY_START]) {
					$company[USERINFO_COMPANY_START] = substr($company[USERINFO_COMPANY_START], 0, strrpos($company[USERINFO_COMPANY_START], "-"));
				}
				if (isset($company[USERINFO_COMPANY_END]) && $company[USERINFO_COMPANY_END]) {
					$company[USERINFO_COMPANY_END] = substr($company[USERINFO_COMPANY_END], 0, strrpos($company[USERINFO_COMPANY_END], "-"));
				}
			}
		}
		return true;
	}

	
	private function _fillBasicInfo($entity) {
		$this->_user_basic_info = array();
		if ($entity) {
			if (isset($entity["nickname"])) $this->_user_basic_info[USERINFO_NICKNAME] = $entity["nickname"];
			//if (isset($entity["nickLastModified"])) $this->_user_basic_info[USERINFO_NICKNAME_LAST_UPDATE_TIME] = $entity["nickLastModified"];
			if (isset($entity["avatarVersion"])) $this->_user_basic_info[USERINFO_AVATAR_VERSION] = $entity["avatarVersion"];
			if (isset($entity["sex"])) $this->_user_basic_info[USERINFO_GENDER] = $entity["sex"];
			if (isset($entity["birthday"]) && $entity["birthday"] != "1900-01-01") $this->_user_basic_info[USERINFO_BIRTH_DAY] = $entity["birthday"];
			if (isset($entity["blood"])) $this->_user_basic_info[USERINFO_BLOOD] = $entity["blood"];
			if (isset($entity["nationality"])) $this->_user_basic_info[USERINFO_NATIONALITY] = $entity["nationality"];
			if (isset($entity["locality"])) $this->_user_basic_info[USERINFO_LOCATION] = $entity["locality"];
			if (isset($entity["hometown"])) $this->_user_basic_info[USERINFO_HOMETOWN] = $entity["hometown"];
			if (isset($entity["registerTime"])) $this->_user_basic_info[USERINFO_REG_TIME] = $entity["registerTime"];
			if (isset($entity["registerIp"])) $this->_user_basic_info[USERINFO_REG_IP] = $entity["registerIp"];
			if (isset($entity["keywords"])) $this->_user_basic_info[USERINFO_KEYWORDS] = $entity["keywords"];
			if (isset($entity["searchFlag"])) $this->_user_basic_info[USERINFO_SEARCHABLE] = $entity["searchFlag"];

			if (isset($entity["reserve2"])) {
				$reserve2 = $entity["reserve2"];
				$this->_user_basic_info[USERINFO_AUTHED_FLAG] = $reserve2 % 10;
				$this->_user_basic_info[USERINFO_LOCKED_FLAG] = intval($reserve2 / 10) % 10;
			}

			if (isset($entity["activeFlag"])) {
				$flag = $entity["activeFlag"];
				if ($flag >= 10) {
					$this->_user_basic_info[USERINFO_ACTIVE_FLAG] = $flag % 10;
					$this->_user_basic_info[USERINFO_AUTHED_FLAG] = intval($flag / 10) % 10;
					// 修正之前将激活标志和认证标志都放在activeFlag中的数据
					$entity = array();
					$entity["activeFlag"] = $this->_user_basic_info[USERINFO_ACTIVE_FLAG];
					$entity["reserve2"] = $this->_user_basic_info[USERINFO_AUTHED_FLAG];
					SDOApiPeopleService::updateBasicInfo($this->_user_id, $entity);
				} else {
					$this->_user_basic_info[USERINFO_ACTIVE_FLAG] = $flag;
				}
			}
		}
	}


	private function _checkBasicInfo() {
		if ($this->_user_basic_info === null) {
			if (!$this->_user_id) {
				$this->_user_basic_info = array();
			} else {
				if (defined("USERINFO_ENABLE_CACHE")) {
					$cache = new UCLibCache(USERINFO_CACHE_NAME, false);
					$result = $cache->read($this->_user_id.".basic");
					if ($result) {
						$this->_user_basic_info = json_decode($result, true);
						if ($this->_user_basic_info) {
							$cache->close();
							return true;
						}
					}
				}

				UCLibLogger::info("get basic info:".$this->_user_id);
				$result = SDOApiPeopleService::getBasicInfo($this->_user_id);
				if (SDOSocket::isError($result) || $result["code"] != 0)  {
					self::$_error_no = 2;
					if (defined("USERINFO_ENABLE_CACHE")) {
						$cache->close();
					}
					return false;
				}
				$entity = SDOApiHelper::getOneResult($result, null);
				$this->_fillBasicInfo($entity);

				if (defined("USERINFO_ENABLE_CACHE")) {
					$cache->write($this->_user_id.".basic", json_encode($this->_user_basic_info), USERINFO_CACHE_EXPIRE);
					$cache->close();
				}
			}
		}
		return true;
	}

	private function _fillExtendInfo($entity) {
		$this->_user_extend_info = array();
		if ($entity) {
			if (isset($entity["hobby"])) $this->_user_extend_info[USERINFO_INTERESTS] = $entity["hobby"];
			if (isset($entity["idol"])) $this->_user_extend_info[USERINFO_IDOLS] = $entity["idol"];
			if (isset($entity["recentVision"])) $this->_user_extend_info[USERINFO_VISION] = $entity["recentVision"];
			if (isset($entity["motto"])) $this->_user_extend_info[USERINFO_MOTTO] = $entity["motto"];
			if (isset($entity["favoriteMovieTv"])) $this->_user_extend_info[USERINFO_FAV_MOVIES] = $entity["favoriteMovieTv"];
			if (isset($entity["favoriteSport"])) $this->_user_extend_info[USERINFO_FAV_SPORTS] = $entity["favoriteSport"];
			if (isset($entity["favoriteBook"])) $this->_user_extend_info[USERINFO_FAV_BOOKS] = $entity["favoriteBook"];
			if (isset($entity["favoriteMusic"])) $this->_user_extend_info[USERINFO_FAV_MUSIC] = $entity["favoriteMusic"];
			if (isset($entity["qq"])) $this->_user_extend_info[USERINFO_QQ] = $entity["qq"];
			if (isset($entity["msn"])) $this->_user_extend_info[USERINFO_MSN] = $entity["msn"];
			if (isset($entity["mail"])) $this->_user_extend_info[USERINFO_EMAIL] = $entity["mail"];
			if (isset($entity["mobile"])) $this->_user_extend_info[USERINFO_MOBILE] = $entity["mobile"];
			if (isset($entity["tel"])) $this->_user_extend_info[USERINFO_PHONE] = $entity["tel"];
			if (isset($entity["webStation"])) $this->_user_extend_info[USERINFO_WEBSITE] = $entity["webStation"];
			if (isset($entity["address"])) $this->_user_extend_info[USERINFO_ADDRESS] = $entity["address"];
			if (isset($entity["zipcode"])) $this->_user_extend_info[USERINFO_ZIPCODE] = $entity["zipcode"];
		}
	}

	private function _checkExtendInfo() {
		if ($this->_user_extend_info === null) {
			if (!$this->_user_id) {
				$this->_user_extend_info = array();
			} else {
				if (defined("USERINFO_ENABLE_CACHE")) {
					$cache = new UCLibCache(USERINFO_CACHE_NAME, false);
					$result = $cache->read($this->_user_id.".extend");
					if ($result) {
						$this->_user_extend_info = json_decode($result, true);
						if ($this->_user_extend_info) {						
							$cache->close();
							return true;
						}
					}
				}
				UCLibLogger::info("get extend info:".$this->_user_id);
				$this->_user_extend_info = array();
				$result = SDOApiPeopleService::getExtendInfo($this->_user_id);
				if (SDOSocket::isError($result) || $result["code"] != 0)  {
					self::$_error_no = 2;
					if (defined("USERINFO_ENABLE_CACHE")) {
						$cache->close();
					}
					return false;
				}
				$entity = SDOApiHelper::getOneResult($result);
				$this->_fillExtendInfo($entity);

				if (defined("USERINFO_ENABLE_CACHE")) {
					$cache->write($this->_user_id.".extend", json_encode($this->_user_extend_info), USERINFO_CACHE_EXPIRE);
					$cache->close();
				}
			}
		}
		return true;
	}

	/**
	  * 保存基本信息
	  */	
	private function _saveBasicInfo($info) {
		if (!$info) {
			self::$_error_no = 1;
			return false;
		}
		$entity = array();

		if (isset($info[USERINFO_NICKNAME])) {
			$entity["nickname"] = $info[USERINFO_NICKNAME];
			$entity["activeFlag"] = "1"; // 激活用户
		}
		
		
		if (isset($info[USERINFO_ACTIVE_FLAG])) $entity["activeFlag"] = $info[USERINFO_ACTIVE_FLAG];

		if (isset($info[USERINFO_AUTHED_FLAG]) || isset($info[USERINFO_LOCKED_FLAG])) {

			$auth_flag = $this->getValue(USERINFO_AUTHED_FLAG);
			if (isset($info[USERINFO_AUTHED_FLAG])) $auth_flag = $info[USERINFO_AUTHED_FLAG];
			$lock_flag = $this->getValue(USERINFO_LOCKED_FLAG);
			if (isset($info[USERINFO_LOCKED_FLAG])) $lock_flag = $info[USERINFO_LOCKED_FLAG];

			$entity["reserve2"] = $lock_flag.$auth_flag;
		}

		//if (isset($info[USERINFO_NICKNAME_LAST_UPDATE_TIME])) $entity["nickLastModified"] = $info[USERINFO_NICKNAME_LAST_UPDATE_TIME];
		if (isset($info[USERINFO_AVATAR_VERSION])) $entity["avatarVersion"] = $info[USERINFO_AVATAR_VERSION];
		if (isset($info[USERINFO_GENDER])) $entity["sex"] = $info[USERINFO_GENDER];
		if (isset($info[USERINFO_BIRTH_DAY])) $entity["birthday"] = $info[USERINFO_BIRTH_DAY];
		if (isset($info[USERINFO_BLOOD])) $entity["blood"] = $info[USERINFO_BLOOD];
		if (isset($info[USERINFO_NATIONALITY])) $entity["nationality"] = $info[USERINFO_NATIONALITY];

		if (isset($info[USERINFO_LOCATION])) $entity["locality"] = $info[USERINFO_LOCATION];
		if (isset($info[USERINFO_HOMETOWN])) $entity["hometown"] = $info[USERINFO_HOMETOWN];
		//if (isset($info[USERINFO_REG_TIME])) $entity["registerTime"] = $info[USERINFO_REG_TIME];
		//if (isset($info[USERINFO_REG_IP])) $entity["registerIp"] = $info[USERINFO_REG_IP];
		if (isset($info[USERINFO_KEYWORDS])) $entity["keywords"] = $info[USERINFO_KEYWORDS];

		if (isset($info[USERINFO_SEARCHABLE])) $entity["searchFlag"] = $info[USERINFO_SEARCHABLE];
		$entity["registerIp"] = get_client_ip();

		$ret = SDOApiPeopleService::updateBasicInfo($this->_user_id, $entity);

		if (SDOSocket::isError($ret) || $ret["data"]["success"] != "true"){
			self::$_error_no = 3;
			#self::$_error_no = 2;
			return false;
		}
		
		if (defined("USERINFO_ENABLE_CACHE")) {
			$cache = new UCLibCache(USERINFO_CACHE_NAME);
			$cache->delete($this->_user_id.".basic");
		}
		return true;
	}

	/**
	  * 保存扩展信息
	  */
	private function _saveExtendInfo($info) {
		if (!$info) {
			self::$_error_no = 1;
			return false;
		}

		$entity = array();
		if (isset($info[USERINFO_INTERESTS])) $entity["hobby"] = $info[USERINFO_INTERESTS];
		if (isset($info[USERINFO_IDOLS])) $entity["idol"] = $info[USERINFO_IDOLS];
		if (isset($info[USERINFO_VISION])) $entity["recentVision"] = $info[USERINFO_VISION];
		if (isset($info[USERINFO_MOTTO])) $entity["motto"] = $info[USERINFO_MOTTO];
		if (isset($info[USERINFO_FAV_MOVIES])) $entity["favoriteMovieTv"] = $info[USERINFO_FAV_MOVIES];
		if (isset($info[USERINFO_FAV_SPORTS])) $entity["favoriteSport"] = $info[USERINFO_FAV_SPORTS];
		if (isset($info[USERINFO_FAV_BOOKS])) $entity["favoriteBook"] = $info[USERINFO_FAV_BOOKS];
		if (isset($info[USERINFO_FAV_MUSIC])) $entity["favoriteMusic"] = $info[USERINFO_FAV_MUSIC];
		if (isset($info[USERINFO_QQ])) $entity["qq"] = $info[USERINFO_QQ];
		if (isset($info[USERINFO_MSN])) $entity["msn"] = $info[USERINFO_MSN];
		if (isset($info[USERINFO_EMAIL])) $entity["mail"] = $info[USERINFO_EMAIL];
		if (isset($info[USERINFO_MOBILE])) $entity["mobile"] = $info[USERINFO_MOBILE];
		if (isset($info[USERINFO_PHONE])) $entity["tel"] = $info[USERINFO_PHONE];
		if (isset($info[USERINFO_WEBSITE])) $entity["webStation"] = $info[USERINFO_WEBSITE];
		if (isset($info[USERINFO_ADDRESS])) $entity["address"] = $info[USERINFO_ADDRESS];
		if (isset($info[USERINFO_ZIPCODE])) $entity["zipcode"] = $info[USERINFO_ZIPCODE];
		if (SDOSocket::isError(SDOApiPeopleService::updateExtendInfo($this->_user_id, $entity))){
			self::$_error_no = 2;
			return false;
		}
		if (defined("USERINFO_ENABLE_CACHE")) {
			$cache = new UCLibCache(USERINFO_CACHE_NAME);
			$cache->delete($this->_user_id.".extend");
		}
		return true;
	}
}

/*
var_dump(UCModUserInfo::getUserInfo(1, array(USERINFO_NICKNAME, USERINFO_GENDER_DESC, USERINFO_AVATAR_30)));
var_dump(UCModUserInfo::getUserInfos(array(1, 2, 3), array(USERINFO_NICKNAME, USERINFO_GENDER_DESC, USERINFO_AVATAR_30, USERINFO_MSN)));

$user = UCModUserInfo::getUser(1);
var_dump( $user->getFullAccess(2));


$user = UCModUserInfo::getUser(5864186);
$user->deleteSchool(3);

echo $user->getCompletePercent(USERINFO_COMPLETE_BASIC)."<br>";
echo $user->getCompletePercent(USERINFO_COMPLETE_CONTACTS)."<br>";
echo $user->getCompletePercent(USERINFO_COMPLETE_INTERESTS)."<br>";
echo $user->getCompletePercent(USERINFO_COMPLETE_EDUCATION)."<br>";
echo $user->getCompletePercent()."<br>";*/
//$user->setAccessAuthorities( array(USERINFO_ACCESS_AUTHORITY_HOME => USERINFO_ACCESS_PRIVATE));
//$user->_checkAccess();
//var_dump($user->getSchoolList());
//$user->update(array(USERINFO_WEBSITE=>"sdo.com"));
/*$user->addSchool( array(USERINFO_ACTION_TYPE => 0,
	            USERINFO_SCHOOL_NAME => "清华大学",
	           USERINFO_SCHOOL_COLLAGE => "计算机系",
	           USERINFO_SCHOOL_CLASS => "3班",
	      USERINFO_SCHOOL_START => "1981"));*/
	//	  $user->getSchoolList();
//var_dump($users);
//var_dump(UCModUserInfo::getUserInfos(array(123, 456), array(USERINFO_NICKNAME, USERINFO_GENDER_DESC, USERINFO_AVATAR_30))); */